package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.CertificationAuditMapper;
import com.jzo2o.customer.mapper.CertificationMapper;
import com.jzo2o.customer.model.domain.CertificationAudit;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.service.ICertificationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class CertificationServiceImpl extends ServiceImpl<CertificationMapper, WorkerCertification> implements ICertificationService {

    @Resource
    CertificationMapper certificationMapper;
    @Resource
    private CertificationAuditMapper certificationAuditMapper;


    //实名认证接口
    @Override
    public void audit(WorkerCertificationAuditAddReqDTO workerCertificationAuditAddReqDTO) {

        //1.新增申请资质认证记录
        CertificationAudit certificationAudit = BeanUtil.toBean(workerCertificationAuditAddReqDTO, CertificationAudit.class);
        certificationAuditMapper.insert(certificationAudit);
        //查询认证记录
        Long serveProviderId = workerCertificationAuditAddReqDTO.getServeProviderId();
        WorkerCertification workerCertification = this.getById(serveProviderId);

        if (ObjectUtil.isNotNull(workerCertification)) {
            //2.将认证信息状态更新为认证中
            workerCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            this.updateById(workerCertification);
        } else {
            workerCertification = new WorkerCertification();
            workerCertification.setId(serveProviderId);
            workerCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            this.save(workerCertification);
        }
    }

    //查询最新驳回原因
    @Override
    public String rejectReason() {
        String rejectReason = certificationMapper.getrejectReason();
        return rejectReason;
    }
}